package org.jetnuke.modules.survey;

import org.apache.log4j.Logger;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.dao.PollCheckDAO;
import org.jetnuke.dao.PollDAO;
import org.jetnuke.dao.PollDataDAO;
import org.jetnuke.entities.Poll;
import org.jetnuke.entities.PollCheck;
import org.jetnuke.entities.PollData;
import org.jetnuke.util.JetUtil;

public class DoVote implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(DoVote.class);

	int id;

	int opt = -1;

	public void setId(int id) {
		this.id = id;
	}

	public void setOpt(int opt) {
		this.opt = opt;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo,
	 *      java.lang.String, org.jetnuke.common.IUserInfo,
	 *      org.jetnuke.common.IAdminInfo)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		if (opt > -1) {
			GregorianCalendar gc = new GregorianCalendar();
			gc.add(Calendar.MINUTE, -30);

			Date dt1 = gc.getTime();
			PollCheckDAO pcm = DataAccessDriver.getInst().newPollCheckDAO();
			pcm.deleteTimeLessThen(dt1);
			PollDAO pm = DataAccessDriver.getInst().newJPollDAO();
			PollDataDAO pdm = DataAccessDriver.getInst().newPollDataDAO();
			// Iterator it = poll_checkPeer.doSelect(
			// new Criteria().add(poll_checkPeer.TIME, dt1,
			// Criteria.LESS_EQUAL)).iterator();
			// while (it.hasNext()) {
			// poll_check poll_check = (poll_check) it.next();
			// poll_checkPeer.doDelete(poll_check);
			// }
			String ip = request.getRemoteAddr();
			List found = pcm.selectByIP_Poll(ip, id);
			// poll_checkPeer.doSelect(new Criteria().add(
			// poll_checkPeer.IP, ip).add(poll_checkPeer.POLLID, id)
			// .setLimit(1));
			boolean valid = true;
			if (!found.isEmpty()) {
				valid = false;
			} else {
				PollCheck check = new PollCheck();
				check.setPOLLID(id);
				check.setIP(ip);
				check.setTIME(new Date());
				pcm.addNew(check);
				// check.save();
			}
			if (valid) {
				PollData dt = pdm.retrieveByPK(id, opt);
				dt.setOPTIONCOUNT(dt.getOPTIONCOUNT() + 1);
				pdm.update(dt);
				// dt.save();

				Poll poll = pm.retrieveByPK(id);
				poll.setVOTERS(poll.getVOTERS() + 1);
				// poll.save();
				pm.update(poll);
			}
		}
		JetUtil.redirect(vc, response, request.getContextPath()
				+ "/do/survey/showresults?id=" + id);

		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return null;
	}

}
